package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.client.admin.IAdminClient;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.AuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.admin.AdUser;
import com.heima.model.common.pojos.admin.ApUserRealname;
import com.heima.model.common.pojos.user.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.AuthService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements AuthService {
    @Autowired
    private IAdminClient adminClient;
    @Autowired
    private ApUserMapper apUserMapper;


    /**
     * 用户审核分页查询
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult pageQuery(AuthDto authDto) {
        //参数校验
        if (authDto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        authDto.checkParam();//分页参数检查
        //组装Wrapper
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        //状态
        if (authDto.getStatus() != null) {
            wrapper.eq(ApUserRealname::getStatus, authDto.getStatus());
        }
        //按发布时间倒序排列
        wrapper.orderByDesc(ApUserRealname::getSubmitedTime);
        // 构建分页page
        Page<ApUserRealname> page = new Page<>(authDto.getPage(), authDto.getSize());

        // 进行分页条件查询
        page(page, wrapper);
        //封装返回结果
        PageResponseResult res = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        res.setData(page.getRecords());
        return res;
    }

    /**
     * 审核驳回
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto authDto) {
        ApUserRealname userRealname = getById(authDto.getId());
        if (userRealname != null) {
            userRealname.setStatus((short) 2);
            userRealname.setReason(authDto.getMsg());
            updateById(userRealname);
            AdUser adUser = new AdUser();
            //赋值非id字段
            BeanUtils.copyProperties(userRealname, adUser);
            adUser.setStatus(0);//设置状态为暂时不可用
            adminClient.saveAdUser(adUser);

        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核通过
     *
     * @param authDto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto authDto) {
        ApUserRealname userRealname = getById(authDto.getId());
        ApUser apUser = apUserMapper.selectById(authDto.getId());
        if (userRealname != null) {
            userRealname.setStatus((short) 9);
            updateById(userRealname);
            AdUser adUser = new AdUser();
            //把数据放入adUser
            BeanUtils.copyProperties(apUser,adUser);
            adUser.setNickname(apUser.getName().substring(0,2));//设置昵称,截取前两个字符
            adUser.setStatus(9);//设置状态为正常可用
            adminClient.saveAdUser(adUser);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
